package cn.com.simple.learning.rabbitmq.controller;

import cn.com.simple.learning.core.constants.RabbitMQConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author Jianxing.Jia
 * @date 2022/5/22
 * @description 描述信息
 */
@Slf4j
@RestController
public class TestController {

    public final StreamBridge streamBridge;
    private final RabbitTemplate rabbitTemplate;

    public TestController(StreamBridge streamBridge, RabbitTemplate rabbitTemplate) {
        this.streamBridge = streamBridge;
        this.rabbitTemplate = rabbitTemplate;
    }

    @RequestMapping("/test")
    public void test(String message) {
        log.info("开始发送消息：{}", message);
        System.out.println(streamBridge);
        // rabbitTemplate.convertAndSend(RabbitMQConstants.EXCHANGE, RabbitMQConstants.ROUTING_KEY, message);
    }

    @RequestMapping("/sendMessage")
    public void sendMessage(String message) {
        log.info("开始发送消息：{}", message);
        rabbitTemplate.convertAndSend(RabbitMQConstants.EXCHANGE, RabbitMQConstants.ROUTING_KEY, message);
    }

    @RequestMapping("/delay/sendMessage")
    public void sendDelayMessage(String message, Integer delayTime) {
        log.info("开始发送延迟消息，当前时间：{}，message：{}，消息延迟时长：{}", LocalDateTime.now(), message, delayTime);
        rabbitTemplate.convertAndSend(RabbitMQConstants.DELAY_EXCHANGE, RabbitMQConstants.DELAY_ROUTING_KEY, message, msg -> {
            MessageProperties properties = msg.getMessageProperties();
            properties.setDelay(delayTime);
            return msg;
        });
    }
}
